package com.mifengs.commonbase.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mifengs.commonbase.R;
import com.mifengs.commonbase.adapter.SelectAdapter;
import com.mifengs.commonbase.util.UIUtil;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by lihaoqi on 2018/10/24.
 */

public class PopBottomSelectView extends Dialog {


    private RecyclerView mSelectView;

    private List<String> selectDatas;

    private SelectAdapter mSelectAdapter;

    private OnSelectItemClickListener mOnSelectItemClickListener;

    public PopBottomSelectView(@NonNull Context context) {
        super(context, R.style.dialog);
        init(context);
    }

    private void init(Context context) {
        setContentView(R.layout.common_bottom_select_view);
        findViewById(R.id.ll_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
        mSelectView = findViewById(R.id.rv_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setAutoMeasureEnabled(true);
        mSelectView.setLayoutManager(linearLayoutManager);
        mSelectAdapter = new SelectAdapter();
        mSelectView.setAdapter(mSelectAdapter);
        mSelectView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context)
                .color(Color.parseColor("#eeeeee"))
                .margin(UIUtil.dp2px(19),UIUtil.dp2px(19))
                .size(UIUtil.dp2px(1))
                .build());
    }


    public void setSelectData(String[] selectStrings) {
        this.selectDatas = Arrays.asList(selectStrings);
        mSelectAdapter.setNewData(selectDatas);
        mSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mOnSelectItemClickListener!=null)
                    mOnSelectItemClickListener.onItemClick(position);
            }
        });
    }


    public void setItemOnClick(OnSelectItemClickListener listener){
        this.mOnSelectItemClickListener = listener;
    }



    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }


    public interface OnSelectItemClickListener{
        void onItemClick(int position);
    }

}
